 
/*  wrtdd.c
    used by tm40.c
    Stephen D. Anderson --- December 21, 1994

    Function writes tag, descriptor , and date to the field instrum.
    It does the following in order:

    Checks whether field instrum exists, does search if not.

    Opens a window to get tag of up to 8 characters, waits for user
    entry of characters.  Fills remaining positions with spaces.
    Packs the characters and puts them into array "msg_data".

    Asks user for descriptor of up to 16 characters, waits for user
    entry of characters.  Fills remaining positions with spaces.
    Packs the characters and puts them into array "msg_data" after
    the packed tag.

    Asks user for date as either "day, month, year" numbers.  Or
    else allows user to enter "s" to use the computer's date.  Puts
    date information into array "msg_data" after the packed descriptor.

    Sends HART command 18 to write the data.  Reports errors if command 
    failed.  Reports success if OK.  Waits for user keystroke to return
    to calling program.

    ON ENTRY:   no arguments.

    ON EXIT:    nothing is returned.

    REVISION LOG:


*/

#include "tm40.h"
#include "pup.h"

void wrtdd()
{
    extern COM com;
    extern byte status1,status2;
    extern byte addr[];     /*  info needed to talk to field instrum. */
    extern byte polling_addr;
    extern byte found;     /*  indicates field instrument has been found. */
    extern byte search(void);    
    extern int hartcmd(byte *, byte, byte *, byte, byte *, byte);
    extern void err(byte);
    extern void err1(byte);
    extern char pup(byte, byte *, byte *, byte);

    int i,j;
    byte msg_data[41];  /*  field instrum internal message. */
    byte message[40];
    byte dump[41];
    struct date today;

    /*  need field intr info? */
    if (!found) found = search();
    if (!found) return;

    /*  get tag.  allow only upper case printable characters. */
    Wopen(5,12,14,68,2,WHITE|_BROWN,WHITE|_BROWN);
    WputCen("\n\nEnter Tag to Store in Field");
    WputCen("\nInstrument (up to 8 characters):");
    Wputs("\n\n              ");
    KwGetFmt(message, "!-U!********");

    /*  quit?  */
    if (TcxlErr == W_ESCPRESS)
    {
        Wclose(); return;
    }

    /*  pad remaining 8 positions with spaces. */
    i = 8 - strlen(message);
    for (j=1; j<=i; ++j) strcat(message, " ");

    /*  pack it. */
    PACK(message, msg_data, 8);

    /*  get descriptor.  allow only upper case printable characters. */
    Wclear();
    WputCen("\n\nEnter Descriptor to Store in Field");
    WputCen("\nInstrument (up to 16 characters):");
    Wputs("\n\n              ");
    KwGetFmt(message, "!-U!****************");

    /*  quit?  */
    if (TcxlErr == W_ESCPRESS)
    {
        Wclose(); return;
    }

    /*  pad the remaining 16 positions with spaces. */
    i = 16 - strlen(message);
    for (j=1; j<=i; ++j) strcat(message, " ");

    /*  pack it and add result to msg_data.  */
    PACK(message, &msg_data[6], 16);

    /*  get date. */
    Wclear();
    WputCen("\n\nEnter the date as 3 numbers:");
    WputCen("\nDay, Month, Year or enter just 'S'");
    WputCen("\nto use system's date");
    Wputs("\n\n                ");
    KwGetFmt(message, "!U!!-R!< 0123Ss>!R!#','< 01>#','<12>###");

    /*  quit?  */
    if (TcxlErr == W_ESCPRESS)
    {
        Wclose(); return;
    }

    if (message[0] == 'S')
    {
        /*  user chooses today's date. */
        getdate(&today);
        msg_data[18] = today.da_day;
        msg_data[19] = today.da_mon;
        msg_data[20] = today.da_year - 1900;
    }
    else
    {
        msg_data[18] = atoi(strleft(message,2));
        msg_data[19] = atoi(strmid(message,2,2));
        msg_data[20] = atoi(strright(message,4)) - 1900;
    }

    /*  send it. */
    i = hartcmd(msg_data, 21, dump, 21, addr, 18);

    if ((i & 1) != 0)
    {
        /*  command failed. */
        delay(1000); err1(18); Wclose(); return;
    }

    /*  byte count correct? */
    if ((i/256) != 21)
    {
        /*  no.  report error. */
        err(18); Wclose(); return;
    }

    /*  report error or success.  */
    Wclear();
    if (status1 != 0)
    {
        /*  error.  */
        err(18);
        WputCen("\n\nTag, Descriptor, Date");
        WputCen("\nmay not have been written.");
    }
    else
    {
        /*  success.  */
        WputCen("\n\nTag, Descriptor, Date Written");
    }
    WputCen("\n\nHit any key to resume");
    KeyWait(); Wclose(); return;
}

